package com.houtai.service.course;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdCourse;
import com.houtai.bean.dto.course.PageCourse;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdCourseDao;
import com.houtai.dao.TdSchoolDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class CourseService {
    private static Logger logger = Logger.getLogger(CourseService.class);
    @Autowired
    private TdCourseDao tdCourseDao;
    @Autowired
    private TdSchoolDao tdSchoolDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找课程
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findCoursePage(PageCourse page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findCoursePage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "course_id desc");
            List<TdCourse> lists = tdCourseDao.findTdCourseList(page);
            PageInfo<TdCourse> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("course_title", page.getCourse_title());
            map.put("school_id", page.getSchool_id());
            map.put("state", page.getState());
            map.put("type_id", page.getType_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找课程" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   添加课程
     * @param     tdCourse
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo insertTdCourse(TdCourse tdCourse, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTdCourse:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdCourseDao.insertTdCourse(tdCourse);
            tdSchoolDao.updateTdSchoolCourseSize(tdCourse.getSchool_id(), 1);
            retInfo.setMark("0");
            retInfo.setTip("创建成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加课程,id:" + tdCourse.getCourse_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改课程
     * @param     tdCourse
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTdCourse(TdCourse tdCourse, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdCourse:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdCourseDao.updateTdCourse(tdCourse);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改课程,id:" + tdCourse.getCourse_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   删除课程
     * @param     course_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo deleteTdCourse(int course_id, int state, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":deleteTdCourse:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCourse tdCourse = tdCourseDao.findTdCourseDetail(course_id);
            tdSchoolDao.updateTdSchoolCourseSize(tdCourse.getSchool_id(), -1);
            tdCourseDao.updateTdCourseState(course_id, state);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改课程状态,id:" + course_id
                     + "状态：" + state,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdCourse findTdCourseById(Integer course_id) {
        return tdCourseDao.findTdCourseDetail(course_id);
    }

}
